package cn.remexs.flink.udf.datatime;


import cn.hutool.core.date.LocalDateTimeUtil;
import org.apache.flink.table.functions.ScalarFunction;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * long 转时间
 */
public class LongToDateTimeStr extends ScalarFunction {

    public String eval(Long longDate, String timeZone) {
        String longDateStr = longDate.toString();
        if (longDateStr.length() > 13) {
            longDateStr = longDateStr.substring(0, 13);
        }
        timeZone = null == timeZone ? "Asia/Shanghai" : timeZone;
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.valueOf(longDateStr)), ZoneId.of(timeZone));
        return LocalDateTimeUtil.format(localDateTime, "yyyy-MM-dd HH:mm:ss");
    }
}
